// my/pages/mzhAddress/mzhAddress.js
var http = require("../../../utils/https.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressList:[
      {
        "id":1,
        "name":"西瓜",
        "phone":"17106245252",
        "address":"浙江省杭州市余杭区XXX号"
      },
      {
        "id":2,
        "name":"西瓜",
        "phone":"17106245252",
        "address":"浙江省杭州市余杭区XXX号"
      },
      {
        "id":3,
        "name":"西瓜",
        "phone":"17106245252",
        "address":"浙江省杭州市余杭区XXX号"
      },
      {
        "id":4,
        "name":"西瓜",
        "phone":"17106245252",
        "address":"浙江省杭州市余杭区XXX号"
      },
    ],
    userInfo:{},
    userId:'',
    address:[]
  },
  toEdit(e){
    console.log(e.currentTarget.dataset.id);
    var id = e.currentTarget.dataset.id
    wx.redirectTo({
      url: '/my/pages/mzhAddressEdit/mzhAddressEdit?id='+id,
    })
  },
  toAdd(){
    wx.redirectTo({
      url: '/my/pages/mzhAddressAdd/mzhAddressAdd?userId='+this.data.userId,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this 
    var userInfo = wx.getStorageSync('userInfo')
    if(userInfo==''||userInfo==null||userInfo.userId==''||userInfo.userId==null){
        wx.showToast({
          title: '认证过期',
          duration:1000
        })
        setTimeout(() => {
            wx.redirectTo({
              url: '/index/pages/mzhLogin/mzhLogin',
            })
          }, 1500);
    }else{
        that.setData({
            userInfo:userInfo,
            userId:userInfo.userId
        })
    }
    var that = this
    http.GET(`/user/address/listByUserId?userId=${that.data.userId}`, {}, function(res) {
        if(res.code===200){
            that.setData({
                address:res.data
            })
        }else{
            wx.showToast({
              title: '登录过期',
              duration:1000
            })
            setTimeout(() => {
                wx.redirectTo({
                  url: '/index/pages/mzhLogin/mzhLogin',
                })
              }, 1500);

        }
        // console.log('成功:', res);
    }, function(err) {
        console.error('失败:', err);
    });
    
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})